<?php


namespace BoojasrFramework\DependencyInjection\Compiler;


use BoojasrFramework\ServicesContainer\ContainerBuilder;
use BoojasrFramework\ServicesContainer\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Console\Command\Command;

class AddCommandPass implements CompilerPassInterface
{

    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition("command_service")){
            return ;
        }

        $commandService = $container->getDefinition("command_service");
        $taggedServices = $container->findTaggedServiceIds('command',true);
        foreach ($taggedServices as $id => $commandDescription){
            $command = $container->get($id);
            if ($command instanceof Command){
                $commandService->addMethodCall("addCommand",[$command]);
            }
        }
    }
}
